package com.example.asuper.check;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements View.OnClickListener {
    private EditText ed_username;
    private EditText ed_pwd;
    private Button bt_submit;
    private Button bt_cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ed_username = (EditText) findViewById(R.id.ed_login_username);
        ed_pwd = (EditText) findViewById(R.id.ed_login_pwd);
        bt_submit = (Button) findViewById(R.id.bt_login_submit);
        bt_cancel = (Button) findViewById(R.id.bt_login_cancel);
        bt_submit.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        boolean isLogin=ReadSharedPreferences();
        if (isLogin){
            gotoMain();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login_submit:
                String name = ed_username.getText().toString();
                String pwd = ed_pwd.getText().toString();
                if (WriteSharedPreferences(name, pwd)) {
                    Toast.makeText(this, "登陆成功！", Toast.LENGTH_LONG).show();
                    gotoMain();
                }
                break;
            case R.id.bt_login_cancel:
                ed_username.setFocusable(true);
                ed_username.requestFocus();
                ed_username.setFocusableInTouchMode(true);
                ed_username.setText("");
                ed_pwd.setText("");

                break;
        }
    }

    //写入登陆信息
    private boolean WriteSharedPreferences(String strName, String strPassword) {
        try {
            SharedPreferences user = getSharedPreferences("user_info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = user.edit();
            editor.putString("NAME", strName);
            editor.putString("PWD", strPassword);
            editor.commit();
        } catch (Exception e) {
            Toast.makeText(this, "登陆失败！", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    //读取登陆信息
    private boolean ReadSharedPreferences(){
        try{
            String strName,strPassword;
            SharedPreferences   user = getSharedPreferences("user_info",0);
            strName = user.getString("NAME","");
            strPassword=user.getString("PWD","");
            if (strName!=null&&!strName.equals("")){
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            return false;
        }
    }

    //跳转到主页面
    private void gotoMain(){
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        this.finish();
    }
}
